﻿var jWaitingHTML = '';
jWaitingHTML = jWaitingHTML + '<div id="waiting_layer" class="waiting-layer">';
jWaitingHTML = jWaitingHTML + '    <div class="waiting-bg"></div>';
jWaitingHTML = jWaitingHTML + '    <div class="waiting-content animation-pulse">';
jWaitingHTML = jWaitingHTML + '        <div class="waiting-wrap">';
jWaitingHTML = jWaitingHTML + '            <i class="fa fa-cog fa-spin fa-5x text-danger"></i>';
jWaitingHTML = jWaitingHTML + '            <span>Xin chờ trong giây lát...</span>';
jWaitingHTML = jWaitingHTML + '        </div>';
jWaitingHTML = jWaitingHTML + '    </div>';
jWaitingHTML = jWaitingHTML + '</div>';

var jWaiting = {
    callWaiting: function () {
        $('#waiting_layer').remove();

        $('body').append(jWaitingHTML);

        var top = ($(window).height() - $('#waiting_layer > div.waiting-content').outerHeight()) / 2;
        var left = ($(window).width() - $('#waiting_layer > div.waiting-content').outerWidth()) / 2;

        $('#waiting_layer').css('left', 0).css('display', 'none');
        $('#waiting_layer > div.waiting-content').css('top', top).css('left', left);
        $('#waiting_layer').fadeIn('normal');
    },
    clearWaiting: function () {
        $('#waiting_layer').css('left', '-9999px').css('display', 'block');
        $('#waiting_layer > div.waiting-content').css('top', 0).css('left', -9999);
    },
    callWaitingMessSuccess: function (mess, url, autoclose) {
        var me = this;
        if (!($('#waiting_layer').length > 0)) {
            $('body').append(jWaitingHTML);
        }
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > span').html(mess);

        var top = ($(window).height() - $('#waiting_layer > div.waiting-content').outerHeight()) / 2;
        var left = ($(window).width() - $('#waiting_layer > div.waiting-content').outerWidth()) / 2;
        $('#waiting_layer').css('left', 0).css('display', 'none');
        $('#waiting_layer > div.waiting-content').css('top', top).css('left', left);

        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('fa-cog');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('fa-spin');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('fa-times');

        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').addClass('fa-check');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').addClass('success');

        $('#waiting_layer > div.waiting-content').removeClass('animation-pulse');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > span').addClass('success');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').addClass('success');

        $('#waiting_layer').fadeIn('normal');
        if (autoclose) {
            var flagauto = setInterval(function () {
                me.clearWaiting();
                clearInterval(flagauto);
                if (url.length > 0) {
                    if (url == 'reload')
                        location.reload();
                    else
                        window.location.href = url;
                }
            }, 1000);

            //            $("#waiting_layer > div.waiting-bg").bind("click", function () {
            //                clearInterval(flagauto);
            //                me.clearWaiting();
            //            });
        } else {
            $("#waiting_layer > div.waiting-bg").bind("click", function () {
                me.clearWaiting();
                if (url.length > 0) {
                    if (url == 'reload')
                        location.reload();
                    else
                        window.location.href = url;
                }
            });
        }

    },
    callWaitingMessError: function (mess) {
        var me = this;
        console.log('call error : ' + mess);
        if (!($('#waiting_layer').length > 0)) {
            $('body').append(jWaitingHTML);
        }
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > span').html(mess);

        var top = ($(window).height() - $('#waiting_layer > div.waiting-content').outerHeight()) / 2;
        var left = ($(window).width() - $('#waiting_layer > div.waiting-content').outerWidth()) / 2;

        /*console.log('screenW  = ' + ($(window).width()));
        console.log('boxwidth = ' + ($('#waiting_layer > div.waiting-content').outerWidth()));
        console.log('screenW - boxwidth = ' + ($(window).width() - $('#waiting_layer > div.waiting-content').outerWidth()));*/
        
        $('#waiting_layer').css('left', 0).css('display', 'none');
        $('#waiting_layer > div.waiting-content').css('top', top).css('left', left);

        console.log('top : ' + top);
        console.log('left : ' + left);

        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('fa-cog');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('fa-spin');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('fa-check');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').removeClass('success');
        $('#waiting_layer > div.waiting-content > div.waiting-wrap > i').addClass('fa-times');

        $('#waiting_layer > div.waiting-content > div.waiting-wrap > span').removeClass('success');
        $('#waiting_layer > div.waiting-content').removeClass('animation-pulse');

        $('#waiting_layer').fadeIn('normal');

        $("#waiting_layer > div.waiting-bg").bind("click", function () {
            me.clearWaiting();
        });
    }
};

//jWaiting.callWaiting();
//jWaiting.callWaitingMessSuccess('Đăng nhập thành công...', '', false);
//jWaiting.callWaitingMessError('Có lỗi xảy ra...', '');